package rpg.avatar
{
	import flash.utils.getTimer;

	import rpg.Utils.GameUtils;
	import rpg.Utils.Val;
	import rpg.animation.AvatarDescription;
	import rpg.core.RenderComponent;
	import rpg.data.HeroData;
	import rpg.factory.GameFactory;
	import rpg.manager.GameManager;
	import rpg.render.RenderData;
	import rpg.role.Hero;

	public class AvatarComponent extends RenderComponent
	{
		public var avatar : Avatar;
		public var hero : Hero;

		private var _state : int;
		private var _dir : int;

		public var duration : int;

		public function AvatarComponent(type : *)
		{
			super(type);
		}

		/**
		 * 添加
		 *
		 */
		override public function notifyAdded() : void
		{
			super.notifyAdded();
			render.zIndex = Val.MIDDLE;
			hero = this.owner as Hero;
			GameManager.getInstance().addMouseComponent(this);
			//render.addShow();
		}

		public function changeAvatar(avatarDes : AvatarDescription , parts : Array) : void
		{
			if(avatar)
				avatar.destroy();
			avatar = GameFactory.getInstance().createAvatar(avatarDes , parts);
			render.addChild(avatar.render);
			_state = Val.STAND;
			avatar.goto(Val.actionStates[_state] , Val.SOUTH);
			duration = 0;
		}

		override public function update(delay : uint) : void
		{
			if(avatar == null)
				return;
			avatar.tryNext(delay , duration);
			avatar.updateRender(hero.mapX - GameUtils.viewX , hero.mapY - GameUtils.viewY , offsetX , offsetY);
		}

		public function get y() : int
		{
			return hero.mapY;
		}

		public function get x() : int
		{
			return hero.mapX;
		}

		public function get state() : int
		{
			return _state;
		}

		public function set state(value : int) : void
		{
			if(_state == value)
				return;
			_state = value;
			playAction(Val.actionStates[_state] , _dir);
		}

		public function get dir() : int
		{
			return _dir;
		}

		public function set dir(value : int) : void
		{
			if(_dir == value)
				return;
			_dir = value;
			playAction(Val.actionStates[_state] , _dir);
		}

		public function playAction(action : String , curDir : int) : void
		{
			if(!avatar)
				return;
			avatar.goto(action , curDir);
		}


		/**
		 * 模糊检测
		 * @param mouseX
		 * @param mouseY
		 * @return
		 *
		 */
		public function checkIsMouseIn(mouseX : int , mouseY : int) : Boolean
		{
			if(!avatar)
				return false;
			return avatar.checkIsMouseIn(mouseX , mouseY);
		}

		/**
		 * 精确检测
		 * @param mouseX
		 * @param mouseY
		 * @return
		 *
		 */
		public function checkIsReaLMouseIn(mouseX : int , mouseY : int) : Boolean
		{
			if(!avatar)
				return false;
			return avatar.checkIsReaLMouseIn(mouseX , mouseY);
		}

		public function get isEnd() : Boolean
		{
			if(!avatar)
				return false;
			return avatar.isEnd;
		}

		override public function destroy() : void
		{
			super.destroy();
			avatar.destroy();
			avatar = null;
			hero = null;
			GameManager.getInstance().removeMouseComponent(this);
		}
	}
}